package com.xh.one2one.knowledgeSystem.controller;

import com.xh.core.util.StringUtils;
import com.xh.one2one.knowledgeSystem.service.IKnowledgeManagerService;
import com.xh.one2one.knowledgeSystem.model.KnowledgeManager;
import com.xh.one2one.knowledgeSystem.bean.request.KnowledgeManagerRequest;
import com.xh.core.bean.XhExportHeaderBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;

import com.xh.core.controller.BaseController;

/**
 * <p>
 * 知识元管理 前端控制器
 * </p>
 *
 * @author tw
 * @since 2020-12-22
 */
@RestController
@RequestMapping("/admin/knowledgeManager")
public class KnowledgeManagerController extends BaseController {

    @Autowired
    private IKnowledgeManagerService knowledgeManagerService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public AjaxResult queryPage( @RequestBody KnowledgeManagerRequest bean){

        XhPage page = new XhPage();
        page.setCurrent(bean.getCurrent());
        page.setSize(bean.getSize());

        page=knowledgeManagerService.queryKnowledgeManagerPage(page,bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",page);
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/query")
    public AjaxResult query(KnowledgeManagerRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",knowledgeManagerService.queryKnowledgeManager(bean));
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @PostMapping(value="/exportAll")
    public AjaxResult exportAll(KnowledgeManagerRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=knowledgeManagerService.exportAll(request,exportHeaderBeans,bean,"queryKnowledgeManagerPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param knowledgeManager 实体对象
     * @return
     */
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated @RequestBody KnowledgeManager knowledgeManager){
        if(knowledgeManager.getId()==null){
            knowledgeManagerService.insert(knowledgeManager);
        }else{
            knowledgeManagerService.update(knowledgeManager);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 保存
     * @param request 实体对象
     * @return
     */
    @PostMapping(value="/save")
    public AjaxResult save(@Validated @RequestBody KnowledgeManagerRequest request){
        if(CollectionUtils.isEmpty(request.getKnowledgeIds())){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误！");
        }
        knowledgeManagerService.save(request);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        knowledgeManagerService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 删除操作
     * @param request
     * @return
     */
    @PostMapping("/deleteAllById")
    @Transactional
    public AjaxResult deleteById(KnowledgeManagerRequest request){
        knowledgeManagerService.deleteAllById(request);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        knowledgeManagerService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }


    /**
     * 刷新数据
     * @return
     */
    @PostMapping("/refresh")
    public AjaxResult refresh(){
        knowledgeManagerService.refreshData();
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }
}

